import { useReducer } from 'react';
/**
 * 定义初始状态信息
 * @loading 加载标记
 */
export interface IstateType {
  activeKeys: Array<string>;
  activeKey: Array<string>;
  expertsInfo: Object;
  expertQualifies: Record<string, any>[];
}

/**
 * 更新专家库信息操作枚举
 * @UPDATE_ACTIVE_KEY 更新折叠面板数据
 */
export enum IActionEnum {
  UPDATE_ACTIVE_KEY = 'updateActiveKey',
  UPDATE_EXPERTS_INFO = 'updateExpertsInfo',
  UPDATE_EXPERTS_QUALIFIES = 'expertQualifies',
}

/**
 * 定义更新合同信息操作匹配
 * @type 操作类型
 * @payload 传递数据
 */
export interface IActionType {
  type: IActionEnum;
  payload?: any;
}

/** 定义专家库新增和编辑状态管理 */
export const useExpertsUpdateReducer = () => {
  /** 初始化状态 */
  const initState: IstateType = {
    activeKeys: ['baseInfo', 'documentInfo', 'certificateInfo', 'phoneInfo', 'otherInfo'],
    activeKey: ['baseInfo', 'documentInfo', 'certificateInfo', 'phoneInfo', 'otherInfo'],
    expertsInfo: {},
    expertQualifies: [],
  };

  /** 状态管理逻辑处理 */
  const updateReducer = (state: IstateType, action: IActionType) => {
    switch (action?.type) {
      case IActionEnum.UPDATE_ACTIVE_KEY:
        return {
          ...state,
          activeKey: action?.payload,
        };
      case IActionEnum.UPDATE_EXPERTS_INFO:
        return {
          ...state,
          expertsInfo: action?.payload,
        };
      case IActionEnum.UPDATE_EXPERTS_QUALIFIES:
        return {
          ...state,
          expertQualifies: action?.payload,
        };
      default:
        return {
          ...state,
          ...initState,
        };
    }
  };

  const [state, dispatch] = useReducer(updateReducer, initState);
  return {
    ...state,
    dispatch,
  };
};
